package weaver.interfaces.expense.action.gzc;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.util.ExpenseCompare;
import weaver.interfaces.util.ExpenseCompareUtil;
import weaver.interfaces.util.ExpenseInfoUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
/**
 * 
 * @ClassName: CYSJSG_ExpenseAction
 * @Description: 5.常用试剂申购单
 * @author xiyufei
 * @date 2017-3-20 上午11:19:21 
 *
 */
public class CYSJSG_ExpenseAction  extends BaseBean implements Action{

 
	public String execute(RequestInfo requestInfo) { 
		try { 
			String workflowid = requestInfo.getWorkflowid();
			String requestid = requestInfo.getRequestid();
			writeLog("进入CYSJSG_ExpenseAction-----------"+requestid);
			RecordSet rs = new RecordSet(); 
			rs.executeSql("select formid from workflow_base where id = " + workflowid);
			rs.next();
			String formid = rs.getString("formid");
			String formtable = "formtable_main_" + formid.replaceAll("-", "");
			String iskt="0";
			String ccode = "";
			double amount = 0;
			String cexpenseitemcode = "";
			String citemcode = "";
			String ly = "";
			String iyear = ""; 
			//System.out.println("select  id,ks,cexpenseitemcode,citemcode,zje,zjly,sqrq,kth from "+formtable+" where requestid="+requestid);
			rs.execute("select id,ks,zcxm,xm,zjine,zj,sqrq,kth1 from "+formtable+" where requestid="+requestid);
			rs.next(); 
			ExpenseInfoUtil expenseInfoUtil = new ExpenseInfoUtil();
			ccode =  expenseInfoUtil.getKsbm(Util.null2String(rs.getString("ks"))); 
			amount =  Util.getDoubleValue(rs.getString("zjine"),0); 
			citemcode=  Util.null2String(rs.getString("xm")); 
			cexpenseitemcode=  Util.null2String(rs.getString("zcxm"));  
			String kth =  Util.null2String(rs.getString("kth1"));  
			ly =  Util.null2String(rs.getString("zj"));  
			iyear =  Util.null2String(rs.getString("sqrq")).substring(0,4); 
			ExpenseCompareUtil  expenseCompareUtil= new  ExpenseCompare();
			if("0".equals(ly)||"6".equals(ly)){//资金来源为课题或者临床试验经费时 课题号+项目支出
				iskt="1";
//				if(!expenseCompareUtil.compareExpense2(workflowid, cexpenseitemcode,kth,amount,iyear)){
//					requestInfo.getRequestManager().setMessageid("1111111111");
//					requestInfo.getRequestManager().setMessagecontent("课题号:"+kth+" ,支出项目:"+cexpenseitemcode+"的预算不足"); 
//					return FAILURE_AND_CONTINUE;
//				}
				if(!"".equals(cexpenseitemcode)){
					if(!expenseCompareUtil.compareExpense2(workflowid, cexpenseitemcode,kth,amount,iyear)){
						requestInfo.getRequestManager().setMessageid("1111111111");
						requestInfo.getRequestManager().setMessagecontent("课题号:"+kth+" ,支出项目:"+expenseInfoUtil.getCItemName(cexpenseitemcode)+"的预算不足"); 
						return SUCCESS;
					}
				}else{
					if(!expenseCompareUtil.compareExpense3(workflowid,cexpenseitemcode,kth,amount,iyear)){
						requestInfo.getRequestManager().setMessageid("1111111111");
						requestInfo.getRequestManager().setMessagecontent("课题号:"+kth+"的预算不足"); 
						return SUCCESS;
					}
				}
			}
			
			if("3".equals(ly)){//医院经费 科室+支出项目+项目控制
				if(!expenseCompareUtil.compareExpense1(workflowid,ccode,cexpenseitemcode,citemcode,amount,iyear)){
					requestInfo.getRequestManager().setMessageid("1111111111");
					requestInfo.getRequestManager().setMessagecontent("科室:"+expenseInfoUtil.getCname(ccode)+",支出项目"+expenseInfoUtil.getCItemName(cexpenseitemcode)+",项目:"+expenseInfoUtil.getCtn(citemcode)+"的预算不足"); 
					return SUCCESS;
				}
			}
			
			if("1".equals(ly)){//财政拨款 课题号
				iskt="1";
				//cexpenseitemcode = "0303";
				if(!expenseCompareUtil.compareExpense3(workflowid,cexpenseitemcode,kth,amount,iyear)){
					requestInfo.getRequestManager().setMessageid("1111111111");
					requestInfo.getRequestManager().setMessagecontent("课题号:"+kth+"的预算不足"); 
					return SUCCESS;
				}
			}
			
			if("2".equals(ly)){//教育拨款和研究生经费 支出项目
				if(!expenseCompareUtil.compareExpense4(workflowid,cexpenseitemcode,amount,iyear)){
					requestInfo.getRequestManager().setMessageid("1111111111");
					requestInfo.getRequestManager().setMessagecontent("支出项目:"+cexpenseitemcode+"的预算不足"); 
					return SUCCESS;
				}
			}
			
			if(!"5".equals(ly)){//非会议类
				if(!expenseInfoUtil.subtractExpense(workflowid, ccode, cexpenseitemcode, citemcode, kth, amount, iyear,iskt)){
					requestInfo.getRequestManager().setMessageid("1111111111");
					requestInfo.getRequestManager().setMessagecontent("预算更新失败"); 
					return SUCCESS;
				}
			}
 
	
			
		} catch (Exception e) {
			requestInfo.getRequestManager().setMessageid("1111111111");
			requestInfo.getRequestManager().setMessagecontent(e.getMessage()); 
			return SUCCESS;
		}
	
		return SUCCESS;
	}

}
